//
//  XLScheduleView.m
//  industry
//
//  Created by xl on 2020/12/18.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLScheduleView.h"
#import "XLScheduleViewProtocol.h"
#import "XLSchedulePresenterProtocol.h"
#import "YQScheduleCustomView.h"
#import "XLScheduleAdapter.h"
#import "XLScheduleStatusView.h"


@interface XLScheduleView ()<XLScheduleViewProtocol,XLScheduleStatusViewDelegate, XLAdapterDelegate>

@property (nonatomic, strong) YQScheduleCustomView *customView;
@property (nonatomic, strong) NSArray *statusArr;
@property (nonatomic, strong) XLTableView *tableView;
@property (nonatomic, strong) XLScheduleAdapter *adapter;
@property (nonatomic, strong)  AUNetErrorView *errorView;
@property (nonatomic, strong) AUToast *toastLoad;
@property (nonatomic, assign) NSInteger type;//0-待办 1-已审批
@property (nonatomic, assign) NSInteger page;


// 无数据视图
//@property (nonatomic, strong)  AUNetErrorView *errorView;

@end
@implementation XLScheduleView

- (void)dealloc {
    XLLog(@"XLScheduleView dealloc");
}

#pragma mark - XLScheduleViewProtocol
- (void)buildView {
    [self addSubview:self.customView];
    [self addSubview:self.tableView];
    
    [_customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(140 + STATUSBAR_HEIGHT);
    }];
    
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self);
        make.top.equalTo(self.customView.mas_bottom);
    }];
    
    WEAKSELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         STRONGSELF
        strongSelf.page = 1;
        XLSend(strongSelf.baseController.presenter, XLSchedulePresenterProtocol, requestApproveList:self.type page:strongSelf.page searchStr:nil);
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        STRONGSELF
        strongSelf.page ++;
        NSString * str = [strongSelf.customView.searchBarView getSearchText];
        if (ISEmptyString(str)) {
            str = nil;
        }
        XLSend(strongSelf.baseController.presenter, XLSchedulePresenterProtocol, requestApproveList:self.type page:strongSelf.page searchStr:str);
    }];
    [self showNoResultView:YES];
    
    
    //TODO
    //开始请求数据
    [XLUserData sharedXLUserData].isApproveFirst = NO;
    XLSend(self.baseController.presenter, XLSchedulePresenterProtocol, requestApproveList:self.type page:1 searchStr:nil);
//    [_adapter setAdapterArray:@[@"1", @"2", @"3"]];
//    [_tableView reloadData];
    
    // TODO: 目前无后台数据，暂时用无数据视图覆盖
    // 正常情况，额外偏移量应该是70；由于self.customView有一根1像素的分隔线，所以这里要多覆盖一点
//    [self addSubview:self.errorView];
//    [self.errorView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).offset(NAVIGATIONBAR_HEIGHT + 79);
//        make.left.bottom.right.equalTo(self);
//    }];
}

- (void)refreshTableView:(NSArray *)dataArr noData:(BOOL)noData {
    [self endRefresh];
    if (noData) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    NSMutableArray *currenArray = [[NSMutableArray alloc] init];
    if (_page != 1) {
        [currenArray addObjectsFromArray:self.adapter.getAdapterArray];
    }
    [currenArray addObjectsFromArray:dataArr];
    if (currenArray.count > 0) {
        [self showNoResultView:NO];
    } else {
        [self showNoResultView:YES];
    }
    [self.adapter setAdapterArray:currenArray];
    [self.tableView reloadData];
}

- (void)requestServerError:(NSError *)error networkReachabilityStatus:(XLNetworkReachabilityStatus)status {
    [self endRefresh];
    if (_adapter.getAdapterArray.count == 0) {
        if (!_errorView || !_errorView.superview) {
            [self showNoResultView:YES];
        }
        _errorView.iconImageView.image = XLIMAGE(@"dbsjbg");
        _errorView.infoTitle = @"咦，出错了~";
    }
}

- (void)beginSearchBar:(NSString *)text {
    
    [_adapter setAdapterArray:nil];
    [_tableView reloadData];
    [self.tableView.mj_footer resetNoMoreData];
    XLSend(self.baseController.presenter, XLSchedulePresenterProtocol, requestApproveList:self.type page:1 searchStr:text);
    
}

- (void)refreshNewRequest {
    
    if ([XLUserData sharedXLUserData].isApproveFirst) {
        [XLUserData sharedXLUserData].isApproveFirst = NO;

        [_customView.segment selectingOneTagWithIndex:0];//重置初始位置
        self.page = 1;
        self.type = 0;

        XLSend(self.baseController.presenter, XLSchedulePresenterProtocol, requestApproveList:self.type page:1 searchStr:nil);
    }
}

//正在加载中的动画
- (void)showLoadingAnimalWithTitle:(NSString *)title {
    self.toastLoad = [XLToast showLoadingTitle:title superView:self];
}

- (void)hideLoadingAnimal {
    if (self.toastLoad) {
        [self.toastLoad dismissToast];
    }
}

- (void)refreshTableViewData {
    [_tableView.mj_header beginRefreshing];
}

#pragma mark - XLScheduleCustomViewDelegate
//切换segment
- (void)segmentChangeIndex:(NSInteger)index {
    
    
    if (index == self.type) {
        return;
    }
    
    [self.customView.searchBarView clearSearchBar];
    
    [_adapter setAdapterArray:nil];
    [_tableView reloadData];
    self.type = index;
    [self.tableView.mj_footer resetNoMoreData];
    XLSend(self.baseController.presenter, XLSchedulePresenterProtocol, requestApproveList:self.type page:1 searchStr:nil);
    
//    self.currentIndex = index;
//    CGFloat height = (index == 0) ? (NAVIGATIONBAR_HEIGHT + 120):(NAVIGATIONBAR_HEIGHT + 70);
//    _customView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
//    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).offset(height);
//    }];
//
//    // TODO：目前无数据，暂时用无数据视图覆盖
//    switch (index) {
//        case 0:
//            self.errorView.infoTitle = @"暂无待办事项";
//            break;
//        case 1:
//            self.errorView.infoTitle = @"暂无已审核事项";
//        default:
//            // 啥也不做
//            break;
//    }
}

//选择状态
- (void)statusChangeIndex:(NSInteger)index {
    
}

#pragma mark - XLAdapterDelegate
- (void)didSelectCellData:(id)cellData {
    
    XLSend(self.baseController.presenter, XLSchedulePresenterProtocol, handleCellData:cellData);
}

- (void)scheduleStatusSelectIndex:(NSInteger)index{
    
}

#pragma mark - Private
- (void)showNoResultView:(BOOL)show
{
    if (show) {
        if(!_errorView) {
            [self.adapter setAdapterArray:nil];//清除数据，避免下拉仍然可见旧数据。
            [UIView animateWithDuration:0 animations:^{
                [self.tableView reloadData];
            }];
            [self.tableView addSubview:self.errorView];
        } else {
            _errorView.iconImageView.image = XLIMAGE(@"dbsjbg");
        }
        NSString *infoStr;
        if (self.type == 0) {
            
            infoStr = @"暂无待办事项";
        } else {
            
            infoStr = @"暂无已审批事项";
        }
        _errorView.infoTitle = infoStr;
    } else {
        if (_errorView) {
            [_errorView dismiss];
            _errorView = nil;
        }
    }
}

#pragma mark - MJrefersh 相关
- (void)endRefresh {
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    }
    if (self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer endRefreshing];
    }
}


#pragma mark - Setter/getter
- (YQScheduleCustomView *)customView {
    if (!_customView) {
        _customView = [[YQScheduleCustomView alloc] init];
        WEAKSELF
        [_customView setSegmentSelectBlock:^(NSInteger index) {
            [weakSelf segmentChangeIndex:index];
        }];
        [_customView.searchBarView setTextFieldChangeCallBack:^(NSString * _Nonnull text) {
            [weakSelf beginSearchBar:text];
        }];
    }
    return _customView;
}

- (XLTableView *)tableView {
    if (!_tableView) {
        _tableView = [[XLTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteBgColor];
        _tableView.delegate = self.adapter;
        _tableView.dataSource = self.adapter;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (XLScheduleAdapter *)adapter {
    if (!_adapter) {
        _adapter = [[XLScheduleAdapter alloc] init];
        _adapter.adapterDelegate = self;
    }
    return _adapter;
}

// 无数据视图
- (AUNetErrorView *)errorView {
    if (!_errorView) {
        _errorView = [[AUNetErrorView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATIONBAR_HEIGHT) style:AUNetErrorStyleIlustration type:AUNetErrorTypeEmpty target:nil action:nil];
        _errorView.iconImageView.image = XLIMAGE(@"dbsjbg");
        _errorView.infoTitle = @"暂无待办事项";
        _errorView.detailTitle = @"";
        _errorView.backgroundColor = [UIColor whiteColor];
        _errorView.infoLabel.textColor = [UIColor colorWithHex:0x9C9C9C];
        _errorView.infoLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        
    }
    return _errorView;
}

@end
